/**
 * Copyright (c) 2006-2007 Jinyi.lu
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of GNU LGPL license by gun.org
 * which accompanies this distribution, and is available at
 * http://gnu.feld-it.at/copyleft/lesser.html
 * 
 * Project Homepage : http://code.google.com/p/demou
 * Source Code: svn checkout http://demou.googlecode.com/svn/trunk/ demou
 * Mailing list : jinyi.lu@gmail.com
 *                stonelu@126.com
 *
 *  
 * Contributors:
 *     JinYi.Lu - initial API and implementation
 */
package cn.tworen.demou;

/**
 * Created on  2007-2-16
 *
 * Title       : FileUtil.java
 * Description : 
 * 
 * @author     : LuJinYi
 * @version    : 1.0
 * @Date       : 2007-2-16
 * History     : 
 * 
 */
public class FileUtil {
	public static String formatToSystemSeparator(String path) {
		if (path == null)
			return null;
		String separator = System.getProperty("file.separator");
		if (separator.equals("/")) {
			return formatToUnixSeparator(path);
		} else {
			return formatToWinSeparator(path);
		}

	}

	public static String formatToUnixSeparator(String path) {
		if (path == null)
			return null;
		if (path.indexOf("\\") > -1) {
			String[] ssSrray = path.replaceAll("\\\\", "#@#").split("#@#");
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < ssSrray.length - 1; i++) {
				sb.append(ssSrray[i] + "/");
			}
			sb.append(ssSrray[ssSrray.length - 1]);
			if (path.endsWith("\\"))
				sb.append("/");
			path = sb.toString();
		}
		return path;
	}

	public static String formatToWinSeparator(String path) {
		if (path == null)
			return null;
		if (path.indexOf("/") > -1) {
			String[] ssSrray = path.replaceAll("/+", "#@#").split("#@#");
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < ssSrray.length - 1; i++) {
				sb.append(ssSrray[i] + "\\");
			}
			sb.append(ssSrray[ssSrray.length - 1]);
			if (path.endsWith("/"))
				sb.append("\\");
			path = sb.toString();
		}
		return path;
	}
}
